using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Text;
using QLVMBAYDomain;
namespace QLVMBAYDatacess
{
    public class QuocGiaDA : DataAccessBase
    {
		#region Constructors
		
		public QuocGiaDA() : base()
        {
			_selectSql = "Select * From QUOCGIA";
            _insertSql = "Insert Into QUOCGIA(MAQG,TENQG,ISMACDINH) Values (@MAQG,@TENQG,@ISMACDINH)";
            _updateSql = "Update QUOCGIA Set TENQG=@TENQG,ISMACDINH=@ISMACDINH Where 1=1 And MAQG=@MAQG";
        	_deleteSql = "Delete From QUOCGIA Where 1=1 And MAQG=@MAQG";
        	_primarSql = "Select * From QUOCGIA Where 1=1 And MAQG=@MAQG";
        }
		
		protected static QuocGiaDA _inst = null;
		public static QuocGiaDA Inst
		{
			get
			{
				if(_inst == null)
					_inst = new QuocGiaDA();
				return _inst;
			}
		}
		
		#endregion
		
        #region Get Methods

		private QuocGia GetCurrent(IDataReader reader, bool isDeep)
		{
			QuocGia ret = new QuocGia();
			ret.MaQG = (string)reader["MAQG"];
            ret.TenQG = (string)reader["TENQG"];
            ret.IsMacDinh = reader["ISMACDINH"] != null && reader["ISMACDINH"] !=DBNull.Value? (int)reader["ISMACDINH"] : 0;
			
			return ret;
		}

		public List<QuocGia> GetAll(bool isDeep)
		{
			DbCommand comm = GetCommand(_selectSql);
            using (Connection)
            {
                using (DbDataReader dr = comm.ExecuteReader())
                {
                    List<QuocGia> ret = new List<QuocGia>();
					while(dr.Read())
					{
						ret.Add(GetCurrent(dr, isDeep));
					}
					return ret;
                }
            }
		}
		
		public QuocGia GetByPrimaryKey(bool isDeep, params object[] args)
		{
			DbCommand cmd = GetCommand(_primarSql);
			if(args.Length < 1)
			{
				throw new Exception();
			}
            SetParam(cmd, "@MAQG", args[0]);

            using (Connection)
            {
                using (DbDataReader dr = cmd.ExecuteReader())
                {
					if(dr.Read())
					{
						return GetCurrent(dr, isDeep);
					}
					return null;
                }
            }
		}
		
		public bool Insert(QuocGia entity)
		{
			DbCommand cmd = GetCommand(_insertSql);
            SetParam(cmd, "@MAQG", entity.MaQG);
            SetParam(cmd, "@TENQG", entity.TenQG);
            SetParam(cmd, "@ISMACDINH", entity.IsMacDinh);
			
            using (Connection)
            {
				int ret = cmd.ExecuteNonQuery();
				return ret > 0;
            }
		}
		
		public bool Update(QuocGia entity)
		{
			DbCommand cmd = GetCommand(_updateSql);
            SetParam(cmd, "@MAQG", entity.MaQG);
            SetParam(cmd, "@TENQG", entity.TenQG);
            SetParam(cmd, "@ISMACDINH", entity.IsMacDinh);
			
            using (Connection)
            {
				int ret = cmd.ExecuteNonQuery();
				return ret > 0;
            }
		}
		
		public bool Delete(QuocGia entity)
		{
			DbCommand cmd = GetCommand(_deleteSql);
            SetParam(cmd, "@MAQG", entity.MaQG);

            using (Connection)
            {
				int ret = cmd.ExecuteNonQuery();
				return ret > 0;
            }
		}
		
		#endregion
    }
}


